package com.yt.product.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import com.yt.product.models.bo.SupplierInfoBO;
import com.yt.product.models.bo.SupplierInfoSaveBO;
import com.yt.product.models.dto.SupplierInfoDTO;
import com.yt.product.models.service.ISupplierInfoService;
import com.yt.product.models.vo.SupplierInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 供应商信息控制器
 *
 * @author zhou
 * @Date 2025-09-05 11:31:21
 */

@RestController
@RequestMapping("/cms/supplierInfo")
@Slf4j
@Api(value = "SupplierInfoController", tags = "供应商信息接口")
public class SupplierInfoController {

    @DubboReference
    private ISupplierInfoService supplierInfoService;

    @ApiOperation(value = "添加供应商信息", notes = "添加供应商信息")
    @PostMapping(value = "/insert")
    public Result insert(SupplierInfoSaveBO saveBO) {
        boolean flag = supplierInfoService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存供应商信息", notes = "保存供应商信息")
    public Result save(SupplierInfoSaveBO saveBO) {
        if (saveBO.getId() == null) {
            //添加
            return insert(saveBO);
        } else {
            return update(saveBO);
        }
    }

    @ApiOperation(value = "修改供应商信息", notes = "修改供应商信息")
    @PostMapping("/update")
    public Result update(SupplierInfoSaveBO saveBO) {
        SupplierInfoDTO dto = new SupplierInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = supplierInfoService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
            , @ApiImplicitParam(name = "status", value = "状态（0禁用，1启用）", required = true, paramType = "query", dataType = "Integer")
    })
    public Result updateStatus(Long id, Integer status) {
        SupplierInfoDTO dto = new SupplierInfoDTO();
        dto.setId(id);
        dto.setStatus(status);
        boolean flag = supplierInfoService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除供应商信息", notes = "删除供应商信息")
    @ApiImplicitParam(name = "id", value = "供应商信息id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id) {
        boolean flag = supplierInfoService.deleteById(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除供应商信息", notes = "批量删除供应商信息")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        supplierInfoService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<SupplierInfoDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        SupplierInfoDTO dto = supplierInfoService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "供应商信息列表", notes = "供应商信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<SupplierInfoDTO>> queryPageList(SupplierInfoBO bo) {
        Wrapper<SupplierInfoDTO> wrapper = new EntityWrapper<>();

        if (bo.getStatus() != null) {
            wrapper.eq("status", bo.getStatus());
        }

        if (bo.getStartDate() != null) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<SupplierInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = supplierInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @PostMapping("/supplierAdd")
    @ApiOperation(value = "供应商新增", notes = "供应商信息新增")
    public Result<Boolean> supplierInfoAdd(@RequestBody SupplierInfoSaveBO bo) {
        if (bo == null) {
            // 若入参为空，返回错误枚举（需与项目中错误码定义一致，示例沿用系统用户的错误码逻辑）
            return new Result<>(CodeEnum.FAIL_100002);
        }
        // 调用Service层方法完成供应商新增，并返回DTO
        boolean flag = supplierInfoService.add(bo);
        return new Result<>(flag);
    }

    @PostMapping("/supplierDelete")
    @ApiOperation(value = "供应商删除", notes = "供应商信息删除")
    @ApiImplicitParam(name = "id", value = "供应商ID", required = true, paramType = "query", dataType = "Long")
    @Transactional
    public Result<Boolean> supplierInfoDelete(@RequestParam Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        //逻辑删除，把状态改为2
        SupplierInfoDTO dto = supplierInfoService.selectById(id);
        dto.setStatus(2);
        boolean flag = supplierInfoService.updateById(dto);
        return new Result<>(flag);

    }

    @PostMapping("/supplierUpdate")
    @ApiOperation(value = "供应商修改", notes = "供应商信息修改")
    @Transactional
    public Result<Boolean> supplierInfoUpdate(@RequestBody SupplierInfoSaveBO bo) {
        if (bo.getId() == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = supplierInfoService.updateSupplier(bo);
        return new Result<>(flag);
    }

    @PostMapping("/querySupplier")
    @ApiOperation(value = "查询供应商", notes = "查询供应商")

    public Result querySupplier() {
        List<SupplierInfoVO> supplierInfoVOS = supplierInfoService.querySupplier();

        return new Result<>(supplierInfoVOS);

    }
}
